package com.start.platform.wx;


import com.alibaba.fastjson2.JSONObject;
import com.start.common.tools.StartRedisTools;
import com.start.platform.core.StartBasePlatformService;
import com.start.platform.core.entity.StartPlatformConfigEntity;
import com.start.platform.core.vo.StartPlatformFansDetail;
import com.start.platform.wx.config.WxOpenRedisConfigStorage;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class WeiXinOpenPlatformService implements StartBasePlatformService<WxOpenService> {

    private static final Map<String, WxOpenService> serviceMap = new HashMap<>();

    @Autowired
    private StartRedisTools startRedisTools;


    private WxOpenService createWxOpenService(StartPlatformConfigEntity entity) {
        WxOpenService wxMaService = new WxOpenServiceImpl();
        wxMaService.setWxOpenConfigStorage(createWxOpenConfigStorage(entity));
        return wxMaService;
    }

    private WxOpenConfigStorage createWxOpenConfigStorage(StartPlatformConfigEntity entity) {
        WxOpenRedisConfigStorage config = new WxOpenRedisConfigStorage(startRedisTools);
        config.setComponentAppId(entity.getAppId());
        config.setComponentAppSecret(entity.getSecret());
        config.setComponentToken(entity.getToken());
        config.setComponentAesKey(entity.getAesKey());
        return config;
    }

    /**
     * 平台类型
     *
     * @return
     */
    @Override
    public String getPlatformType() {
        return SystemConstant.PlatformTypes.WX.OPEN;
    }

    @Override
    public String getPlatformTypeLabel() {
        return "微信开放平台";
    }

    /**
     * 添加配置
     *
     * @param config
     */
    @Override
    public void add(StartPlatformConfigEntity config) {
        if (serviceMap.containsKey(config.getAppId())) {
            return;
        }
        WxOpenService wxMaService = createWxOpenService(config);
        serviceMap.put(config.getAppId(), wxMaService);
    }

    @Override
    public void remove(String appId) {
        if (!serviceMap.containsKey(appId)) {
            return;
        }
        serviceMap.remove(appId);
    }

    /**
     * 修改配置
     *
     * @param appId
     * @param config
     */
    @Override
    public void updateByAppId(String appId, StartPlatformConfigEntity config) {
        if (!serviceMap.containsKey(appId)) {
            return;
        }
        remove(appId);
        add(config);
    }

    /**
     * 是否存在AppId
     *
     * @param appId
     * @return
     */
    @Override
    public boolean hasAppId(String appId) {
        return serviceMap.containsKey(appId);
    }

    /**
     * 获取平台服务
     *
     * @param appId
     * @return
     */
    @Override
    public WxOpenService getPlatformService(String appId) {
        if (!serviceMap.containsKey(appId)) {
            return null;
        }
        return serviceMap.get(appId);
    }



    /**
     * 根据第三方平台code 获取用户信息
     *
     * @param appId
     * @param code
     * @param prop
     * @return
     */
    @Override
    public StartPlatformFansDetail getUserInfoByPlatformCode(String appId, String code, JSONObject prop) {
        return null;
    }

    /**
     * 平台验证token
     *
     * @param appId
     * @return
     */
    @Override
    public Object platformVerifyToken(String appId) {
        return null;
    }
}
